﻿Public Class 维修记录

    '关闭窗体
    Private Sub 维修记录_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        PrRecoForm = Nothing
    End Sub

    '改变窗口大小
    Private Sub 维修记录_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.Width < 806 Then
            Me.Width = 806
        End If

        If Me.Height < 573 Then
            Me.Height = 573
        End If
    End Sub

    '打印按钮
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If DataGridView1.Rows.Count = 0 Then
            MsgBox("未有可打印的维修记录！")
            Exit Sub
        End If

        CType(PrintPreviewDialog1, Form).WindowState = FormWindowState.Maximized

        PrintPreviewDialog1.ShowDialog()

    End Sub

    '确定按扭
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.Rows.Clear()
        If DateTimePicker1.Value.Date <= DateTimePicker2.Value.Date Then

            Dim d1 As Date = DateTimePicker1.Value.Date
            Dim d2 As Date = DateTimePicker2.Value.Date

            Dim sql As String = ""

            If d1 = d2 Then
                d2 = d1.AddDays(1)
                sql = "select * from 维修表 where 始修日期 >= #" + d1 + "# and 始修日期 < #" + d2 + "#"
            Else
                sql = "select * from 维修表 where 始修日期 >= #" + d1 + "# and 始修日期 < #" + d2.AddDays(1) + "#"
            End If

            Dim ds As DataSet = SQLtoDataSet(sql)
            Dim tb As DataTable = ds.Tables(0)
            DataGridView1.Rows.Clear()
            TotalDoller = 0
            If tb.Rows.Count > 0 Then
                DataGridView1.Rows.Clear()
                For i = 0 To tb.Rows.Count - 1
                    Dim rs As DataRow = tb.Rows(i)
                    Dim row1() As String = { _
                                                Trim(rs.Item("ID").ToString), _
                                                Trim(rs.Item("维修单号").ToString), _
                                                Trim(rs.Item("品牌").ToString), _
                                                Trim(rs.Item("型号").ToString), _
                                                Trim(rs.Item("项目").ToString), _
                                                Trim(rs.Item("维修员").ToString), _
                                                Trim(rs.Item("始修日期").ToString), _
                                                Trim(rs.Item("预测费用").ToString), _
                                                Trim(rs.Item("备注").ToString), _
                                                Trim(rs.Item("维修结果").ToString), _
                                                Trim(rs.Item("修讫日期").ToString), _
                                                Trim(rs.Item("实收费用").ToString), _
                                                Trim(rs.Item("修讫备注").ToString), _
                                                Trim(rs.Item("电话").ToString), _
                                                Trim(rs.Item("地址").ToString) _
                                                }

                    DataGridView1.Rows.Add(row1)

                    Try
                        Dim teinte As Integer = Val(Trim(rs.Item("实收费用").ToString))
                        If teinte >= 0 Then
                            TotalDoller += teinte
                        End If
                    Catch ex As Exception

                    End Try
                Next
            End If

        Else
            MsgBox("起始日期必须比终止日期早！")
            DateTimePicker1.Focus()
        End If
    End Sub

    Dim TotalDoller As Integer

    Dim Page_Count As Integer = 7       '每页记录数
    Dim Page_Mulit As Integer = 0        '打印记录的指针
    Dim Page_Total As Integer = 0        '页数
    Dim Page_NowPr As Integer = 1        '页数指针

    Dim ZT_title As Font = New Font(New FontFamily("黑体"), 24)         '标题字体
    Dim ZT_PageC As Font = New Font(New FontFamily("宋体"), 12)         '页脚字体
    Dim ZT_CaleL As Font = New Font(New FontFamily("宋体"), 14)         '汇总字体

    '打印事件
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        '打算页数
        Dim PageCtem As Integer = DataGridView1.Rows.Count
        If PageCtem > Page_Count Then
            '需分页
            Page_Total = PageCtem \ Page_Count + 1
        Else
            '不需分页
            Page_Total = 1
        End If

        '执行打印
        Dim g As Graphics = e.Graphics
        Dim pen As Pen = New Pen(Color.Black)
        Dim rect As Rectangle = e.MarginBounds

        Debug.Print("dfsdfs:" + rect.Width.ToString + "," + rect.Height.ToString)

        '汇总线
        g.DrawLine(Pens.Black, rect.Left - 50, 150, rect.Right + 50, 150)

        If CheckBox1.Checked Then
            g.DrawString("实际总收入：￥" + TotalDoller.ToString, ZT_PageC, Brushes.Black, rect.Left - 50, 130)
        End If


        '打印时间及页码
        g.DrawString("Page " + Page_NowPr.ToString + "/" + Page_Total.ToString + "  打印时间：" + Now.ToString, ZT_PageC, Brushes.Black, rect.Right - 260, rect.Bottom + 20)

        '标题
        g.DrawString("绍康电器商店维修记录报表", ZT_title, Brushes.Black, 220, 60)


        '表头-------

        Dim xx As Integer = rect.Left - 50
        Dim yy As Integer = 180

        g.DrawLine(Pens.Black, rect.Left - 50, yy - 5, rect.Right + 50, yy - 5)           '横线
        g.DrawLine(Pens.Black, rect.Left - 50, yy - 5, rect.Left - 50, yy + 70)           '竖线
        g.DrawLine(Pens.Black, rect.Right + 50, yy - 5, rect.Right + 50, yy + 70)           '竖线

        g.DrawString("单号", ZT_PageC, Brushes.Black, xx, yy) : xx += 130
        g.DrawLine(Pens.Black, xx - 5, yy - 5, xx - 5, yy + 20)

        g.DrawString("品牌型号", ZT_PageC, Brushes.Black, xx, yy) : xx += 130
        g.DrawLine(Pens.Black, xx - 5, yy - 5, xx - 5, yy + 20 + 25 + 25)

        g.DrawString("维修项目", ZT_PageC, Brushes.Black, xx, yy) : xx += 130
        g.DrawLine(Pens.Black, xx - 5, yy - 5, xx - 5, yy + 20)

        g.DrawString("维修人员", ZT_PageC, Brushes.Black, xx, yy) : xx += 100
        g.DrawLine(Pens.Black, xx - 5, yy - 5, xx - 5, yy + 20 + 25)

        g.DrawString("始修日期", ZT_PageC, Brushes.Black, xx, yy) : xx += 130
        g.DrawLine(Pens.Black, xx - 5, yy - 5, xx - 5, yy + 20 + 25)

        g.DrawString("预测费用", ZT_PageC, Brushes.Black, xx, yy) : xx += 100

        g.DrawLine(Pens.Black, rect.Left - 50, yy + 20, rect.Right + 50, yy + 20)           '横线

        yy += 25
        xx = rect.Left - 50

        g.DrawString("报修备注", ZT_PageC, Brushes.Black, xx, yy) : xx += 130 : xx += 130
        g.DrawString("维讫备注", ZT_PageC, Brushes.Black, xx, yy) : xx += 130 : xx += 100
        g.DrawString("修讫日期", ZT_PageC, Brushes.Black, xx, yy) : xx += 130
        g.DrawString("实收费用", ZT_PageC, Brushes.Black, xx, yy) : xx += 100

        g.DrawLine(Pens.Black, rect.Left - 50, yy + 20, rect.Right + 50, yy + 20)           '横线

        yy += 25
        xx = rect.Left - 50

        g.DrawString("客户电话", ZT_PageC, Brushes.Black, xx, yy) : xx += 130 : xx += 130
        g.DrawString("客户地址", ZT_PageC, Brushes.Black, xx, yy)

        g.DrawLine(Pens.Black, rect.Left - 50, yy + 20, rect.Right + 50, yy + 20)           '横线



        '打印记录内容
        For i = (Page_NowPr - 1) * Page_Count To Page_NowPr * Page_Count - 1

            xx = rect.Left - 50
            yy += 65
            Dim dr As DataGridViewRow = DataGridView1.Rows(i)

            '打印第一行-----------------------------------
            g.DrawString(dr.Cells.Item("维修单号").Value.ToString, ZT_PageC, Brushes.Black, xx, yy) : xx += 130
            g.DrawLine(Pens.Black, xx - 5, yy, xx - 5, yy + 20)

            g.DrawString(dr.Cells.Item("品牌").Value.ToString + " " + dr.Cells.Item("型号").Value.ToString, ZT_PageC, Brushes.Black, xx, yy) : xx += 130
            g.DrawLine(Pens.Black, xx - 5, yy, xx - 5, yy + 20 + 25 + 20)

            g.DrawString(dr.Cells.Item("维修项目").Value.ToString, ZT_PageC, Brushes.Black, xx, yy) : xx += 130
            g.DrawLine(Pens.Black, xx - 5, yy, xx - 5, yy + 20)

            g.DrawString(dr.Cells.Item("维修人员").Value.ToString, ZT_PageC, Brushes.Black, xx, yy) : xx += 100
            g.DrawLine(Pens.Black, xx - 5, yy, xx - 5, yy + 20 + 25)

            Dim dt As Date = DateTime.Parse(dr.Cells.Item("始修日期").Value.ToString)
            g.DrawString(dt.ToShortDateString, ZT_PageC, Brushes.Black, xx, yy) : xx += 130
            g.DrawLine(Pens.Black, xx - 5, yy, xx - 5, yy + 20 + 25)

            Dim tm1 As String
            If Trim(dr.Cells.Item("预测费用").Value.ToString) = "-1" Or Trim(dr.Cells.Item("预测费用").Value.ToString) = "" Then
                tm1 = "未确定"
            Else
                tm1 = "￥" + dr.Cells.Item("预测费用").Value.ToString
            End If
            g.DrawString(tm1, ZT_PageC, Brushes.Black, xx, yy) : xx += 100

            g.DrawLine(Pens.Black, rect.Left - 50, yy + 20, rect.Right + 50, yy + 20)           '横线


            '打印第二行-----------------------------------
            yy += 25
            xx = rect.Left - 50

            g.DrawString(dr.Cells.Item("始修备注").Value.ToString, ZT_PageC, Brushes.Black, xx, yy) : xx += 130 : xx += 130
            g.DrawString(dr.Cells.Item("修讫备注").Value.ToString, ZT_PageC, Brushes.Black, xx, yy) : xx += 130 : xx += 100

            Dim temstr As String
            If Trim(dr.Cells.Item("修讫日期").Value.ToString) <> "" Then
                Dim dt2 As Date = DateTime.Parse(dr.Cells.Item("修讫日期").Value.ToString)
                temstr = dt2.ToShortDateString
            Else
                temstr = "未修讫"
            End If
            g.DrawString(temstr, ZT_PageC, Brushes.Black, xx, yy) : xx += 130

            Dim tm2 As String
            If Trim(dr.Cells.Item("实收费用").Value.ToString) = "-1" Or Trim(dr.Cells.Item("实收费用").Value.ToString) = "" Then
                tm2 = "未确定"
            Else
                tm2 = "￥" + dr.Cells.Item("实收费用").Value.ToString
            End If
            g.DrawString(tm2, ZT_PageC, Brushes.Black, xx, yy) : xx += 100

            g.DrawLine(Pens.Black, rect.Left - 50, yy + 20, rect.Right + 50, yy + 20)           '横线



            '打印第三行-----------------------------------
            yy += 25
            xx = rect.Left - 50

            g.DrawString(dr.Cells.Item("客户联系方式").Value.ToString, ZT_PageC, Brushes.Black, xx, yy) : xx += 130 : xx += 130
            g.DrawString(dr.Cells.Item("客户地址").Value.ToString, ZT_PageC, Brushes.Black, xx, yy)

            If i = DataGridView1.Rows.Count - 1 Then Exit For
        Next

        '判断页数
        Dim PageCtem2 As Integer = DataGridView1.Rows.Count

        If Page_NowPr = Page_Total Then
            Page_NowPr = 1
            Page_Total = 0
            Exit Sub
        End If

        If PageCtem2 > Page_Count Then
            '需分页
            Page_NowPr += 1
            e.HasMorePages = True
        End If

    End Sub

    '执行删除操作
    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click
        Dim dc As Integer = DataGridView1.SelectedRows.Count
        If dc > 0 Then
            For i = 0 To dc - 1
                Dim sqlstr As String = "delete from 维修表 where ID =" + DataGridView1.SelectedRows(0).Cells("ID").Value
                SQLupdata(sqlstr)
                DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(0).Index)
            Next
        End If
    End Sub

End Class